package com.team.hiring.talent.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.team.hiring.common.util.DBUtil;
import com.team.hiring.talent.entity.ResumeEntity;

/**
 * 简历的数据库操作
 * 
 * @author lishifa
 *
 */
public class ResumeDao {

	/**
	 * 新增简历
	 * 
	 * @param resumeEntity
	 */
	public int add(ResumeEntity resumeEntity) {
		int row = 0;
		try {
			Connection connection = DBUtil.getConnection();
			String sql = "insert into resume value(null,?,?,?,?)";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, resumeEntity.getTalentId());
			preparedStatement.setString(2, resumeEntity.getIntention());
			preparedStatement.setString(3, resumeEntity.getWorkExperience());
			preparedStatement.setString(4, resumeEntity.getProjectExperience());
			row = preparedStatement.executeUpdate();

			// 关闭连接
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 查询简历信息
	 * 
	 * @param talentId
	 */
	public List<ResumeEntity> resumeList(int talentId) {
		List<ResumeEntity> resumeList = new ArrayList<ResumeEntity>();
		try {
			Connection connection = DBUtil.getConnection();
			String sql = "select * from resume where talent_id=?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, talentId);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ResumeEntity resumeEntity = new ResumeEntity();
				resumeEntity.setId(resultSet.getInt("id"));
				resumeEntity.setTalentId(resultSet.getInt("talent_id"));
				resumeEntity.setIntention(resultSet.getString("intention"));
				resumeEntity.setWorkExperience(resultSet
						.getString("work_experience"));
				resumeEntity.setProjectExperience(resultSet
						.getString("project_experience"));
				resumeList.add(resumeEntity);
			}
			// 关闭连接
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resumeList;
	}

	/**
	 * 简历修改
	 * 
	 * @param resumeEntity
	 * @return
	 */
	public int update(ResumeEntity resumeEntity) {
		int row = 0;
		try {
			Connection connection = DBUtil.getConnection();
			String sql = "update resume set intention=?,work_experience=?,project_experience=? where id=?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setString(1, resumeEntity.getIntention());
			preparedStatement.setString(2, resumeEntity.getWorkExperience());
			preparedStatement.setString(3, resumeEntity.getProjectExperience());
			preparedStatement.setInt(4, resumeEntity.getId());
			row = preparedStatement.executeUpdate();

			// 关闭连接
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 删除简历
	 * 
	 * @param id
	 * @return
	 */
	public int delete(int id) {
		int row = 0;
		try {
			Connection connection = DBUtil.getConnection();
			String sql = "delete from resume where id=?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			row = preparedStatement.executeUpdate();
			
			// 关闭连接
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 根据id查询当前简历的所有信息
	 * 
	 * @param id
	 * @return
	 */
	public ResumeEntity selectById(int id) {
		ResumeEntity resume = null;
		try {
			Connection connection = DBUtil.getConnection();
			String sql = "select * from resume where id=?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				resume = new ResumeEntity();
				resume.setId(resultSet.getInt("id"));
				resume.setTalentId(resultSet.getInt("talent_id"));
				resume.setIntention(resultSet.getString("intention"));
				resume.setWorkExperience(resultSet
						.getString("work_experience"));
				resume.setProjectExperience(resultSet
						.getString("project_experience"));
			}
			// 关闭连接
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resume;
	}

	
	/**
	 * 如果talentEntity不为空，则查找简历信息
	 * @param talentId
	 * @return
	 */
	public List<ResumeEntity> homepageSelectResume(int talentId) {
		List<ResumeEntity> resumeList = new ArrayList<ResumeEntity>();
		try {
			Connection connection = DBUtil.getConnection();
			String sql = "select * from resume where talent_id=?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, talentId);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ResumeEntity resumeEntity = new ResumeEntity();
				resumeEntity.setId(resultSet.getInt("id"));
				resumeEntity.setTalentId(resultSet.getInt("talent_id"));
				resumeEntity.setIntention(resultSet.getString("intention"));
				resumeEntity.setWorkExperience(resultSet
						.getString("work_experience"));
				resumeEntity.setProjectExperience(resultSet
						.getString("project_experience"));
				resumeList.add(resumeEntity);
			}
			// 关闭连接
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resumeList;
	}

}
